﻿using System.Diagnostics;
using HLogger = HK.Core.Logs.Logger;
using HLogLevel = HK.Core.Logs.Protocol.LogLevel;

namespace HK.Core.Extend
{
    /// <summary>
    /// System.Object 日志扩展
    /// </summary>
    public static class SObjectEx
    {
        /// <summary>
        /// 日志输出(一般 - 运行日志)
        /// </summary>
        /// <param name="iScript">当前脚本</param>
        /// <param name="iFormat">日志格式</param>
        /// <param name="iArgs"></param>
        [Conditional("HK_LOG_INFO")]
        public static void Info(this System.Object iScript, string iFormat, params object[] iArgs)
        {
            if (HLogger.LogLevel > HLogLevel.RInfo) {
                return;
            }
            var className = iScript.GetType().Name;
            var log = string.Format(iFormat, iArgs);
            HLogger.Info($"[{className}] {log}");
        }
        
        /// <summary>
        /// 日志输出(一般 - 逻辑日志)
        /// </summary>
        /// <param name="iScript">当前脚本</param>
        /// <param name="iFormat">日志格式</param>
        /// <param name="iArgs"></param>
        public static void LInfo(this System.Object iScript, string iFormat, params object[] iArgs)
        {
            if (HLogger.LogLevel > HLogLevel.LInfo) {
                return;
            }
            var className = iScript.GetType().Name;
            var log = string.Format(iFormat, iArgs);
            HLogger.LInfo($"[{className}] {log}");
        }
        
        /// <summary>
        /// 日志输出(警告日志)
        /// </summary>
        /// <param name="iScript">当前脚本</param>
        /// <param name="iFormat">日志格式</param>
        /// <param name="iArgs"></param>
        [Conditional("HK_LOG_WARNING")]
        public static void Warning(this System.Object iScript, string iFormat, params object[] iArgs)
        {
            if (HLogger.LogLevel > HLogLevel.Warning) {
                return;
            }
            var className = iScript.GetType().Name;
            var log = string.Format(iFormat, iArgs);
            HLogger.Warning($"[{className}] {log}");
        }
        
        /// <summary>
        /// 日志输出(错误日志)
        /// </summary>
        /// <param name="iScript">当前脚本</param>
        /// <param name="iFormat">日志格式</param>
        /// <param name="iArgs"></param>
        public static void Error(this System.Object iScript, string iFormat, params object[] iArgs)
        {
            if (HLogger.LogLevel > HLogLevel.Error) {
                return;
            }
            var className = iScript.GetType().Name;
            var log = string.Format(iFormat, iArgs);
            HLogger.Error($"[{className}] {log}");
        }
        
        /// <summary>
        /// 日志输出(致命日志)
        /// </summary>
        /// <param name="iScript">当前脚本</param>
        /// <param name="iFormat">日志格式</param>
        /// <param name="iArgs"></param>
        public static void Fatal(this System.Object iScript, string iFormat, params object[] iArgs)
        {
            if (HLogger.LogLevel > HLogLevel.Fatal) {
                return;
            }
            var className = iScript.GetType().Name;
            var log = string.Format(iFormat, iArgs);
            HLogger.Fatal($"[{className}] {log}");
        }
    }
}
